<template>
  <div class="page-container">
    <!--工具栏-->
    <div class="toolbar" style="float:left;padding-top:10px;padding-left:15px;">
      <el-form :inline="true" :model="filters" :size="size" ref="filters">
        <el-form-item prop="effectiveRole">
          <el-select v-model="filters.effectiveRole" placeholder="生效组织" style="width:215px">
            <el-option
              v-for="item in effectiveRole"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item prop="indicatorId">
          <el-select v-model="filters.indicatorId" placeholder="指标" style="width:215px">
            <el-option
              v-for="item in indicators"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-row>
          <el-form-item>
            <kt-button
              icon="fa fa-search"
              :label="$t('action.search')"
              type="primary"
              @click="findPage(null)"
            />
          </el-form-item>
          <el-form-item>
            <kt-button icon="fa fa-refresh" :label="$t('action.reset')" type="primary" @click="resetForm('filters')"/>
          </el-form-item>
        </el-row>
        <el-row>
          <el-form-item>
            <kt-button
              label="批量启用"
              type="active"
              @click="handleEnable"
            />
          </el-form-item>
        </el-row>
      </el-form>
    </div>
    <div class="toolbar" style="float:right;padding-top:10px;padding-right:15px;">
      <el-form :inline="true" :size="size">
        <el-form-item>
          <el-button-group>
            <el-tooltip content="刷新" placement="top">
              <el-button icon="fa fa-refresh" @click="findPage(null)"></el-button>
            </el-tooltip>
            <el-tooltip content="列显示" placement="top">
              <el-button icon="fa fa-filter" @click="displayFilterColumnsDialog"></el-button>
            </el-tooltip>
            <el-tooltip content="导出" placement="top">
              <el-button icon="fa fa-file-excel-o" @click="downloadExcel"></el-button>
            </el-tooltip>
          </el-button-group>
        </el-form-item>
      </el-form>
      <!--表格显示列界面-->
      <table-column-filter-dialog
        ref="tableColumnFilterDialog"
        :columns="columns"
        @handleFilterColumns="handleFilterColumns"
      ></table-column-filter-dialog>
    </div>

    <!--表格内容栏-->
    <cy-table
      :height="350"
      :data="pageResult"
      :columns="filterColumns"
      @findPage="findPage"
      @selectionChange="selectionChange"
      :showDeleteOperation="false"
      :showBatchDelete="false"
      @handleEdit="handleEdit"
      ref="CyTable"
    ></cy-table>
    <!--新增匹配条件-->
    <el-dialog
      title="编辑"
      width="40%"
      :visible.sync="editDialogVisible"
      :close-on-click-modal="false"
    >
      <el-form
        :model="dataForm"
        label-width="80px"
        ref="dataForm"
        :size="size"
      >
        <el-form-item label="id" prop="id" v-if="false">
          <el-input v-model="dataForm.id" :disabled="true" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="指标" class="styleReplace" prop="indicatorName">
          <el-input v-model="dataForm.indicatorName" auto-complete="off" :disabled="true" ></el-input>
        </el-form-item>
        <el-form-item label="警戒线规则设置" class="styleReplace" prop="alertValue">
          <el-input v-model="dataForm.alertValue" auto-complete="off" >
            <template slot="append">%</template>
          </el-input>
        </el-form-item>
        <el-form-item label="红线规则设置" class="styleReplace" prop="redlineValue">
          <el-input v-model="dataForm.redlineValue" @input="handleInput" auto-complete="off" >
            <template slot="append">%</template>
          </el-input>
        </el-form-item>
        <el-form-item label="当该指标高于" class="styleReplace" prop="redlineValue">
          <el-input v-model="dataForm.redlineValue" auto-complete="off" :disabled="true" >
            <template slot="append">%时</template>
          </el-input>
        </el-form-item>
        <el-form-item label="发送邮件至" class="styleReplace" prop="warningMailbox">
          <el-input v-model="dataForm.warningMailbox" auto-complete="off" >
            <template slot="append">多个邮箱用;隔开</template>
          </el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" style="text-align:center">
        <el-button :size="size" @click.native="editDialogVisible = false">{{$t('action.cancel')}}</el-button>
        <el-button
          :size="size"
          type="primary"
          @click.native="submitForm"
          :loading="editLoading"
        >{{$t('action.submit')}}
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  //import CyTable from "@/views/Core/CyTable";
  import KtButton from "@/views/Core/KtButton";
  import {format} from "@/utils/datetime";
  import TableColumnFilterDialog from "@/views/Core/TableColumnFilterDialog";
  import CyTable from "@/views/Core/StaticTable";

  export default {
    components: {
      CyTable,
      KtButton,
      TableColumnFilterDialog
    },
    data() {
      return {
        effectiveRole: [{
          value: '平台',
          label: '平台'
        }, {
          value: '商家',
          label: '商家'
        }],
        indicators: [{
          value: '售后订单率',
          label: '售后订单率'
        }, {
          value: '成交金额',
          label: '成交金额'
        }, {
          value: '成交订单数',
          label: '成交订单数'
        }, {
          value: '成交转化率',
          label: '成交转化率'
        }, {
          value: 'PV',
          label: 'PV'
        }, {
          value: 'UV',
          label: 'UV'
        }, {
          value: '商详页UV',
          label: '商详页UV'
        }, {
          value: '新客数',
          label: '新客数'
        }, {
          value: '滞销品数',
          label: '滞销品数'
        }, {
          value: '沉默率',
          label: '沉默率'
        }, {
          value: '流失率',
          label: '流失率'
        }, {
          value: '成交商品数',
          label: '成交商品数'
        }, {
          value: '成交客户数',
          label: '成交客户数'
        }, {
          value: '二跳率',
          label: '二跳率'
        }],
        size: "small",
        filters: {
          endEventValue: "",
          indicatorId: "",
          res: {
            "total": 2,
            "code": "0000",
            "rows": [{
              "id": 1,
              "indicatorName": "售后订单率",
              "alertValueStr": "3.00%",
              "alertValue": "3",
              "redlineValueStr": "5.00%",
              "redlineValue": "5",
              "effectiveOrganization": "平台",
              "warningMailbox":"bbc@bbc.com"
            },
              {
                "id": 2,
                "indicatorName": "成交金额",
                "alertValueStr": "200000.00元",
                "alertValue": "200000",
                "redlineValueStr": "100000.00元",
                "redlineValue": "100000",
                "effectiveOrganization": "平台",
                "warningMailbox":"bbc@bbc.com"
              }]
          }
        },
        columns: [
          {prop: "id", label: "序号", minWidth: 50},
          {prop: "indicatorName", label: "指标", minWidth: 120,showSwitch:true},
          {prop: "endEventName", label: "预警值", minWidth: 120, children:[
              {label:'警戒线指标设置值',prop:'alertValueStr'},
              {label:'红线指标指标值',prop:'redlineValueStr'}
            ]},
          {prop: "effectiveOrganization", label: "生效组织", minWidth: 120},
          {prop: "warningMailbox", label: "预警邮箱", minWidth: 120}
        ],
        filterColumns: [],
        pageRequest: {pageNum: 1, pageSize: 10},
        pageResult: {},

        operation: false, // true:新增, false:编辑
        editDialogVisible: false, // 新增编辑界面是否显示
        editDialogVisibleMatching: false,
        editLoading: false,
        dataFormRules: {
          replyContent: [{required: true, message: "回复内容", trigger: "blur"}]
        },
        // 新增编辑界面数据
        dataForm: {
          alertValueStr: "",
          effectiveOrganization: "",
          indicatorName: "售后订单率",
          redlineValue: "3",
          redlineValueStr:""
        },
        dataFormMatching: {
          matching: ""
        },
        shopList: [],
        statusList: [],
        checked: []
      };
    },
    methods: {
      // 获取分页数据
      findPage: function (data) {
        this.$refs.CyTable.findPageStaticState(this.filters);
      },
      // 显示新增界面
      handleAdd: function () {
        this.editDialogVisible = true;
        this.operation = true;
        this.resetForm("dataForm");
      },
      //选择框
      selectionChange: function (params) {
        this.checked = params.selections
      },
      // 显示编辑界面
      handleEdit: function (params) {
        this.editDialogVisible = true;
        this.operation = false;
        this.$nextTick(function () {
          this.dataForm = Object.assign({}, params.row);
        });
      },
      // 编辑
      submitForm: function () {
        this.$message({message: "操作成功", type: "success"});
        this.editDialogVisible = false;
      },
      //批量操作
      handleEnable: function () {
        if (this.checked.length >=1) {
          this.$message({message: "操作成功", type: "success"});
        } else {
          this.$message({message: '必须选中一条数据', type: 'error'})
        }
      },
      // 时间格式化
      dateFormat: function (row, column, cellValue, index) {
        return format(row[column.property]);
      },

      resetForm(formName) {
        if (this.$refs[formName] != undefined) {
          this.$refs[formName].resetFields();
          this.filters.start_time = "";
          this.filters.end_time = "";
          this.findPage(null);
        }
      },

      // 处理表格列过滤显示
      displayFilterColumnsDialog: function () {
        this.$refs.tableColumnFilterDialog.setDialogVisible(true);
      },
      // 处理表格列过滤显示
      handleFilterColumns: function (data) {
        this.filterColumns = data.filterColumns;
        this.$refs.tableColumnFilterDialog.setDialogVisible(false);
      },
      // 处理表格列过滤显示
      initColumns: function () {
        let temp = [];

        $.each(this.columns, function (key, val) {
          temp.push(val);
        });
        this.filterColumns = temp;
      },
      //列表下载
      downloadExcel() {
        this.$confirm("确定下载列表文件?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            let filename = "换货管理下载";
            this.$refs.CyTable.exportData(
              this.filters,
              this.filterColumns,
              filename
            );
          })
          .catch(() => {
          });
      },
      // 实时更新的
      handleInput: function (e) {
        this.dataForm.redlineValue = e
      }

    },
    mounted() {
      this.initColumns()
    }
  };
</script>

<style>
  .styleReplace .el-form-item__label {
    width: 120px !important
  }

  .styleReplace .el-form-item__content {
    margin-left: 120px !important
  }
</style>
